package com.jxc.action.spgl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VBrand;
import com.jxc.model.vo.VGoodsType;
import com.jxc.model.vo.VRepertory;
import com.jxc.model.vo.VUser;
import com.jxc.service.GoodsTypeServiceI;
import com.jxc.service.RepertoryServiceI;
import com.jxc.service.UserServiceI;
import com.jxc.util.ExceptionUtil;
import com.jxc.util.ResourceUtil;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/spgl")
@Action(value = "goodsTypeAction", results = { @Result(name = "goodsType", location = "/goodsType/goodsType.jsp"),
		@Result(name = "goodsTypeAdd", location = "/goodsType/goodsTypeAdd.jsp"),
		@Result(name = "goodsTypeEdit", location = "/goodsType/goodsTypeEdit.jsp") })
public class GoodsTypeAction extends BaseAction implements ModelDriven<VGoodsType> {
	private final static Logger logger = Logger.getLogger(GoodsTypeAction.class);

	private VGoodsType model = new VGoodsType();

	@Autowired
	private GoodsTypeServiceI goodsTypeService;
	@Autowired
	private UserServiceI userService;
	@Autowired
	private RepertoryServiceI repertoryService;

	public String goodsType() {
		getSubAuths("SPGL_SPLXGL");
		return "goodsType";
	}

	public String goodsTypeAdd() {
		return "goodsTypeAdd";
	}

	public String goodsTypeEdit() {
		return "goodsTypeEdit";
	}
	
	public void noAuth_combobox() {
		
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		if (vuser.getReid() == null) {
			model.setUid(sessionInfo.getUserId());
			List<VGoodsType> vgoodsTypes = goodsTypeService.combobox(model);
			this.writeJson(vgoodsTypes);
		}
		else {
			model.setUid(vuser.getUpid());
			List<VGoodsType> vgoodsTypes = goodsTypeService.combobox(model);
			this.writeJson(vgoodsTypes);
		}
	}

	public void datagrid() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().
				getSession().getAttribute(ResourceUtil.getSessionInfoName());		
			VUser vuser = userService.get(sessionInfo.getUserId());
//			如果当前用户仓库为空,说明是经销商或代理商用户,如果不为空,那么他可以看到他的上级的折扣
		if(vuser.getReid()==null){
			model.setUid(sessionInfo.getUserId());
			this.writeJson(goodsTypeService.datagrid(model));
		}else{
			model.setUid(vuser.getUpid());
			this.writeJson(goodsTypeService.datagrid(model));
		}
	}

	/**
	 * 添加品牌
	 * 
	 */
	public void add() {
		Json j = new Json();
		
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
			model.setUid(sessionInfo.getUserId());
			goodsTypeService.save(model);
			
			j.setMsg("添加成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg("添加失败，商品类型已存在！");
			logger.info(ExceptionUtil.getExceptionMessage(e));
		}
		super.writeJson(j);
	}

	/**
	 * 修改品牌
	 * 
	 */
	public void edit() {
		Json j = new Json();
		try {
			goodsTypeService.update(model);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("编辑失败，商品类型名称已存在！");
		}
		super.writeJson(j);
	}

	/**
	 * 删除品牌
	 * 
	 */
	public void delete() {
		Json j = new Json();
		goodsTypeService.delete(model.getIds());
		j.setSuccess(true);
		j.setMsg("删除成功!");
		super.writeJson(j);
	}

	@Override
	public VGoodsType getModel() {
		return model;
	}

}
